/**
  * 2020/06/11 
  * branch: f-videoUpload-qixiaofei
  * 上传过程中用到的变量
  *  */
import { uploadVideoChunk } from '@/api/index'
export const uploadByPieces = ({ file, pieceSize = 5, progress, success, error }) => {
  /**
    * 2020/06/11 
    * branch: f-videoUpload-qixiaofei
    * 上传过程中用到的变量
    *  */

  const chunkSize = pieceSize * 1024 * 1024 // 5MB一片
  const chunkCount = Math.ceil(file.size / chunkSize) // 总片数
  const readFile = () => {
    /**
      * 2020/06/11 
      * branch: f-videoUpload-qixiaofei
      * 读取视频文件
      *  */



    let fileRederInstance = new FileReader()
    //   fileRederInstance.readAsBinaryString(file)
    fileRederInstance.readAsArrayBuffer(file)

    fileRederInstance.addEventListener('load', e => {
      console.log(3333)
      // feat: 初始化吧一定要为0 or 不要试着去修改，如果去修改 会接连下面会修改
      const { chunk } = getChunkInfo(file, 0, chunkSize)
      /**
        * 2020/06/11 
        * branch: f-videoUpload-qixiaofei
        * chunkCount "总片数"
        *  */

      uploadChunk({ chunk, currentChunk: 1, chunkCount })

    })
  }
  const getChunkInfo = (file, currentChunk, chunkSize) => {
    let start = currentChunk * chunkSize
    let end = Math.min(file.size, start + chunkSize)
    let chunk = file.slice(start, end)
    return { start, end, chunk }
  }

  /**
    * 2020/06/11 
    * branch: f-videoUpload-qixiaofei
    * guid 唯一
    *  */
  function guid () {
    function S4 () {
      return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  }
  let uuid = guid()

  const uploadChunk = (chunkInfo, currentChunk, path) => {
    let videoForm = new FormData()
    videoForm.append('currentChunk', chunkInfo.currentChunk) // 当前数量
    videoForm.append('id', uuid) // 第一次上传的路径
    videoForm.append('totalChunks', chunkInfo.chunkCount) // 拆分总数
    videoForm.append('file', chunkInfo.chunk) // 文件
    videoForm.append('totalSize', file.size) // 文件大小
    videoForm.append('fileType', file.type.split('/')[1]) // 文件
    uploadVideoChunk(videoForm).then(res => {
      if (res.success) {
        /**
         * 2020/06/11 
         * branch: f-videoUpload-qixiaofei
         * 当前分片 == 总拆分 那就上传成功 / or / 继续上传
         *  */
        if (chunkInfo.currentChunk < chunkInfo.chunkCount) {
          chunkInfo.currentChunk++

          /**
           * 2020/06/11 
           * branch: f-videoUpload-qixiaofei
           * 进度条接口暴漏
           *  */
          const { chunk } = getChunkInfo(file, chunkInfo.currentChunk - 1, chunkSize)
          uploadChunk({ chunkInfo, currentChunk: chunkInfo.currentChunk, chunkCount, chunk })
          let gress = Math.ceil(chunkInfo.currentChunk / chunkInfo.chunkCount * 100)
          progress && progress(gress)
        } else {
          /**
            * 2020/06/11 
            * branch: f-videoUpload-qixiaofei
            * 当总数大于等于分片个数的时候
            *  */
          if (chunkInfo.currentChunk >= chunkInfo.chunkCount) {
            // chunkInfo.currentChunk = 1
            success && success(res)
          }
        }
      }
      else {
        error && error(res)
      }
    }).catch((e) => {
      error && error(res)
    })
  }
  readFile() // 开始执行
}
